/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-10-20 09:13:44
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-12 08:53:35
 * @FilePath: \cxl-driving-training\src\main.ts
 */
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import { createPinia } from 'pinia'
import router from './router'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import routerLimit from './router/limit'
import { routerStore } from './store/router.store'
import { userStore } from './store/user.store'

const app = createApp(App)
const pinia = createPinia()

app.use(pinia).use(router).use(ElementPlus).mount('#app')

/**
 * 错误信息打印
 * @param err
 * @param instance
 * @param info
 */
app.config.errorHandler = (err: unknown, instance, info: string) => {
    console.error({ err, instance, info })
}

/**
 * 路由权限判断
 */
new routerLimit().init()

const uStore = userStore()

/**
 * 加载图标；在设置菜单导航icon时，不知道会选择哪种icon。所以全部加载
 */
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
    uStore.icons.push(key)
}

/**
 * 权限自定义指令
 */
app.directive('permission', {
    mounted(el, binding) {
        const { value } = binding
        const permissions = routerStore().permission

        if (value && value instanceof Array && value.length > 0) {
            let flag = true

            value.forEach(item => {
                if (!permissions.includes(item)) {
                    flag = false
                }
            })
            if (!flag) {
                el.parentNode && el.parentNode.removeChild(el)
            }
        } else {
            throw new Error('请设置操作权限标签值')
        }
    }
})

// !打印链接的环境
console.log(import.meta.env.VITE_MODE, import.meta.env.VITE_HOST)
